#include<iostream>
#include<string>
#include<memory>
using namespace std;
//抽象产品类
class Shape
{
public:
    virtual ~Shape()=default;
    virtual void draw()const=0;
    virtual string getName()const=0; 
};
//具体产品类：圆形
class Circle:public Shape
{
public:
    void draw() const override
    {
        cout<<"绘制圆形"<<endl;
    }
    string getName()const override
    {
        return "圆形";
    }
};
//具体产品类：矩形
class Rectangle:public Shape
{
public:
    void draw() const override
    {
        cout<<"绘制矩形"<<endl;
    }
    string getName()const override
    {
        return "矩形";
    }
};
//具体产品类：三角形
class Triangle:public Shape
{
public:
    void draw() const override
    {
        cout<<"绘制三角形"<<endl;
    }
    string getName()const override
    {
        return "三角形";
    }
};

//工厂类
class ShapeFactory
{
public:
    //根据类型创建对应的产品
    static unique_ptr<Shape> createShape(const string shapeType)
    {
        if(shapeType=="circle")
        {
            return make_unique<Circle>();
        }
        else if(shapeType=="rectangle")
        {
            return make_unique<Rectangle>();
        }
        else if(shapeType=="triangle")
        {
            return make_unique<Triangle>();
        }
        else
        {
            cerr<<"无效的形状类型"<<endl;
            return nullptr;
        }
    }
};
int main()
{
    try
    { 
        //创建一个圆形
        auto circle=ShapeFactory::createShape("circle");
        if(circle!=nullptr)
        {
            cout<<circle->getName()<<"已创建"<<endl;
            circle->draw();
        }
        cout<<"-------------------------------------------------"<<endl;
        //创建一个矩形
        auto rectangle=ShapeFactory::createShape("rectangle");
        if(rectangle!=nullptr)
        {
            cout<<rectangle->getName()<<"已创建"<<endl;
            rectangle->draw();
        }
        cout<<"-------------------------------------------------"<<endl;
        //创建一个三角形
        auto triangle=ShapeFactory::createShape("triangle");
        if(triangle!=nullptr)
        {
            cout<<triangle->getName()<<"已创建"<<endl;
            triangle->draw();
        }
        cout<<"-------------------------------------------------"<<endl;
        //创建一个无效的形状
        auto invalidShape=ShapeFactory::createShape("invalidShape");
        if(invalidShape!=nullptr)
        {
            cout<<invalidShape->getName()<<"已创建"<<endl;
            invalidShape->draw();
        }
        cout<<"-------------------------------------------------"<<endl;
    }
    catch(const exception& e)
    {
        cerr<<"程序执行错误"<<e.what()<<endl;
        return 1;
    }
    return 0; 
}